package com.ruoyi.project.ledger.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.ledger.domain.TbTransportWaybillLeaveCheck;
import com.ruoyi.project.ledger.service.ITbTransportWaybillLeaveCheckService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 车辆出厂检查Controller
 *
 * @author gwc
 * @date 2020-04-16
 */
@RestController
@RequestMapping("/ledger/check")
public class TbTransportWaybillLeaveCheckController extends BaseController
{
    @Autowired
    private ITbTransportWaybillLeaveCheckService tbTransportWaybillLeaveCheckService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询车辆出厂检查列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:check:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbTransportWaybillLeaveCheck tbTransportWaybillLeaveCheck)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            tbTransportWaybillLeaveCheck.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        tbTransportWaybillLeaveCheck.setAreas(areas);
        tbTransportWaybillLeaveCheck.setManagementCharacters(managementCharacters);
        startPage();
        List<TbTransportWaybillLeaveCheck> list = tbTransportWaybillLeaveCheckService.selectTbTransportWaybillLeaveCheckList(tbTransportWaybillLeaveCheck);
        return getDataTable(list);
    }

    /**
     * 导出车辆出厂检查列表
     */
    /*@PreAuthorize("@ss.hasPermi('ledger:check:export')")*/
    @Log(title = "车辆出厂检查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbTransportWaybillLeaveCheck tbTransportWaybillLeaveCheck)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            tbTransportWaybillLeaveCheck.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        tbTransportWaybillLeaveCheck.setAreas(areas);
        tbTransportWaybillLeaveCheck.setManagementCharacters(managementCharacters);
        List<TbTransportWaybillLeaveCheck> list = tbTransportWaybillLeaveCheckService.selectTbTransportWaybillLeaveCheckList(tbTransportWaybillLeaveCheck);
        ExcelUtil<TbTransportWaybillLeaveCheck> util = new ExcelUtil<TbTransportWaybillLeaveCheck>(TbTransportWaybillLeaveCheck.class);
        return util.exportExcel(list, "check");
    }

    /**
     * 获取车辆出厂检查详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('ledger:check:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbTransportWaybillLeaveCheckService.selectTbTransportWaybillLeaveCheckById(id));
    }

    /**
     * 新增车辆出厂检查
     */
    @PreAuthorize("@ss.hasPermi('ledger:check:add')")
    @Log(title = "车辆出厂检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbTransportWaybillLeaveCheck tbTransportWaybillLeaveCheck)
    {
        return toAjax(tbTransportWaybillLeaveCheckService.insertTbTransportWaybillLeaveCheck(tbTransportWaybillLeaveCheck));
    }

    /**
     * 修改车辆出厂检查
     */
    @PreAuthorize("@ss.hasPermi('ledger:check:edit')")
    @Log(title = "车辆出厂检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbTransportWaybillLeaveCheck tbTransportWaybillLeaveCheck)
    {
        return toAjax(tbTransportWaybillLeaveCheckService.updateTbTransportWaybillLeaveCheck(tbTransportWaybillLeaveCheck));
    }

    /**
     * 删除车辆出厂检查
     */
    @PreAuthorize("@ss.hasPermi('ledger:check:remove')")
    @Log(title = "车辆出厂检查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbTransportWaybillLeaveCheckService.deleteTbTransportWaybillLeaveCheckByIds(ids));
    }
}
